package com.chrray.position.app.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chrray.position.auth.Authentication;
import com.chrray.position.auth.AuthenticationContext;
import com.chrray.position.domain.FdUser;
import com.chrray.position.enums.YesNoEnum;
import com.chrray.position.exception.ServiceException;
import com.chrray.position.mapper.position.FdFeedbackMapper;
import com.chrray.position.mapper.position.FdUserMapper;
import com.chrray.position.service.position.FdFeedbackService;
import lombok.AllArgsConstructor;
import com.chrray.position.domain.FdFeedback;
import com.chrray.position.domain.vo.FdFeedbackVO;
import com.chrray.position.domain.dto.search.FdFeedbackSearch;
import com.chrray.position.domain.dto.FdFeedbackDTO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 意见反馈表 接口实现类
 *
 * @author wusiwee
 * @email wusiwee@163.com
 * @since 1.0.1 2024-08-07
 */
@Service
@AllArgsConstructor
public class FdFeedbackServiceImpl extends ServiceImpl<FdFeedbackMapper, FdFeedback> implements FdFeedbackService {

    @Resource
    private FdUserMapper fdUserMapper;
    /**
     * 分页查询
     * @param param 查询条件
     * @return 分页列表
     */
    @Override
    public IPage<FdFeedbackVO> pageList(FdFeedbackSearch param) {
        //查询数据
        IPage<FdFeedback> page = page(getPage(param), getWrapper(param));
        IPage<FdFeedbackVO> voPage = new Page<>();
        //数据组装
        voPage.setRecords(page.getRecords().stream().map(r->{
            FdFeedbackVO vo = new FdFeedbackVO();
            BeanUtils.copyProperties(r,vo);
            //数据组装
            setViewParam(r,vo);
            return vo;
        }).collect(Collectors.toList()));
        voPage.setPages(page.getPages());
        voPage.setTotal(page.getTotal());
        voPage.setSize(page.getSize());
        voPage.setCurrent(page.getCurrent());
        return voPage;
    }

    /**
     * 动态条件构建
     * @param param 查询参数
     * @return 条件语句
     */
    private LambdaQueryWrapper<FdFeedback> getWrapper(FdFeedbackSearch param){
        LambdaQueryWrapper<FdFeedback> wrapper = Wrappers.lambdaQuery();
        // 主键ID
        wrapper.eq(param.getId() != null, FdFeedback::getId, param.getId());
        // 用户ID
        wrapper.eq(param.getUserId() != null, FdFeedback::getUserId, param.getUserId());
        // 用户角色:雇工-0,雇主-1
        wrapper.eq(param.getUserRole() != null, FdFeedback::getUserRole, param.getUserRole());
        // 内容
        wrapper.eq(param.getContent() != null && !param.getContent().isEmpty(), FdFeedback::getContent, param.getContent());
        // 照片
        wrapper.eq(param.getPhotos() != null && !param.getPhotos().isEmpty(), FdFeedback::getPhotos, param.getPhotos());
        // 反馈时间
        wrapper.eq(param.getFeedbackTime() != null, FdFeedback::getFeedbackTime, param.getFeedbackTime());
        //
        wrapper.eq(param.getDelFlag() != null, FdFeedback::getDelFlag, param.getDelFlag());
        // 主键倒序
        wrapper.orderByDesc(FdFeedback::getId);
        return wrapper;
    }

    /**
     * 详情
     * @param id 查询条件
     * @return 详情
    */
    @Override
    public FdFeedbackVO info(Long id) {
        // 查询主键详情
        FdFeedback entity = getById(id);
        if (ObjectUtils.isEmpty(entity)){
            throw new ServiceException("查询不到数据");
        }
        // 回显参数封装
        FdFeedbackVO result = new FdFeedbackVO();
        BeanUtils.copyProperties(entity,result);
        // 数据组装
        setViewParam(entity,result);
        return result;
    }

    /**
     * 新增
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean create(FdFeedbackDTO body) {
        Authentication context = AuthenticationContext.context();
        FdFeedback entity = new FdFeedback();
        entity.setContent(body.getContent());
        if(!CollectionUtils.isEmpty(body.getPhotos())) {
            entity.setPhotos(String.join(",",body.getPhotos()));
        }
        entity.setFeedbackTime(new Date());
        entity.setDelFlag(YesNoEnum.NO.getCode());
        entity.setUserId(context.getUserId());
        if(ObjectUtils.isNotEmpty(body.getUserRole())){
            entity.setUserRole(context.getCurrentRole());
        }else {
            entity.setUserRole(body.getUserRole());
        }

        FdUser fdUser = fdUserMapper.selectById(context.getUserId());
        entity.setUserName(fdUser.getName());
        return save(entity);
    }

    /**
     * 批量删除
     * @param idList 主键列表
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean delete(List<Long> idList) {
        //逻辑删除
        //FdFeedback entity = new FdFeedback();
        //entity.setDelFlag(1);
        //return update(entity,new LambdaQueryWrapper<FdFeedback>().in(FdFeedback::getId,idList));

        //物理删除
        return removeByIds(idList);
    }

    /**
     * 获取分页对象
     *
     * @param param 分页参数
     */
    protected IPage<FdFeedback> getPage(FdFeedbackSearch param) {
        return new Page<>(param.getPageNum(), param.getPageSize());
    }

    /**
     * 视图数据赋值
     * @param r 数据
     * @param vo 视图
     */
    private void setViewParam(FdFeedback r, FdFeedbackVO vo) {
        //数据赋值业务..
    }

}
